package main

import (
	"fmt"
	"encoding/json"
)

type Student struct {
	Name string `json:"name"`   //反射机制
	Age int `json:"age"`
	Sal float64 `json:"sal"`
}

func testStruct() {
	s := Student {
		Name : "json串",
		Age : 15,
		Sal : 78.09,
	}

	data,err := json.Marshal(s)

	if err != nil {
		fmt.Println("序列化错误，错误：",err)
		return
	}
	fmt.Println("序列化结果：",string(data))
}

func testMap() {
	
	s := make(map[string]interface{})
	s["age"] = 15
	s["address"] = "陕西"
	s["sal"] = 99999.87

	data,err := json.Marshal(s)

	if err != nil {
		fmt.Println("序列化错误，错误：",err)
		return
	}
	fmt.Println("序列化结果：",string(data))
}

func testSlice()  {
	s := make([]map[string]interface{},0)

	m1 := make(map[string]interface{})
	m1["age"] = 15
	m1["address"] = "陕西"
	m1["sal"] = 99999.87
	s = append(s,m1)

	m2 := make(map[string]interface{})
	m2["age"] = 15
	m2["address"] = "陕西"
	m2["sal"] = 99999.87
	s = append(s,m2)

	data,err := json.Marshal(s)

	if err != nil {
		fmt.Println("序列化错误，错误：",err)
		return
	}
	fmt.Println("序列化结果：",string(data))

}

func main()  {
	testStruct()
	testMap()
	testSlice()
}

